package com.fox0g.service.impl;

import com.fox0g.mapper.*;
import com.fox0g.netty.NettyServerHandler;
import com.fox0g.pojo.Profit;
import com.fox0g.pojo.Tstate;
import com.fox0g.service.FarmService;
import com.fox0g.service.TstateService;
import com.fox0g.websocket.WebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;

/**
* @author 86176
* @description 针对表【tstate(温度)】的数据库操作Service实现
* @createDate 2024-07-07 08:53:44
*/
@Service
public class TstateServiceImpl implements TstateService{
    ArrayList<String> list = new ArrayList<>();
    //    @Autowired
//    ShengxiangService shengxiangService;
    @Autowired
    FarmService farmService;

    @Autowired
    FarmMapper farmMapper;


    @Autowired
    WebSocketHandler webSocketHandler;
    @Autowired
    NettyServerHandler nettyServerHandler;
    @Autowired
    FreshfoodMapper freshfoodMapper;

    @Autowired
    SharebikeMapper sharebikeMapper;

    @Autowired
    TstateMapper tstateMapper;

    @Autowired
    ProfitMapper profitMapper;


    public static String Meg = "";

    @Override
    public void HandleMessage(String message) throws IOException {
        webSocketHandler.send(message);
//        解析温湿度光照强度数据帧
        //HWV122.0I12TS6T123.3T221.0Z
        if (message.matches("HWt\\d+(\\.\\d+)?h\\d+(\\.\\d+)?ls1\\d+(\\.\\d+)?ls2\\d+(\\.\\d+)?Z")) {

            String arrnews1[] = message.split("t");
            String arrnew2[] = arrnews1[1].split("h");
            String arrnew3[] = arrnew2[1].split("ls1");
            String arrnew4[] = arrnew3[1].split("ls2");
            String arrnew5[] = arrnew4[1].split("Z");
//            String arrnew6[] = arrnew5[1].split("Z");
            String V1 = arrnew2[0];  //截取温度
            String I = arrnew3[0];  //电容
            String TS = arrnew4[0]; //光照1
            String T1 = arrnew5[0]; //光照2
//            String T2 = arrnew6[0]; //饮水水质
            LocalTime now = LocalDateTime.now().toLocalTime(); //获取当前时间
            //向前端发送这些数据帧
            webSocketHandler.send("T#" + V1);
            webSocketHandler.send("H" + I);
            webSocketHandler.send("LS1#" + TS);
            webSocketHandler.send("LS2#" + T1);
            System.out.println(V1 + " " + TS + " " + T1 + " ");
//            webSocketHandler.send("sw" + TS);
            //将解析后的数据设置给pojo类
            Tstate tstate = new Tstate();
            tstate.setWd(V1);
            tstate.setCreatTime(now);
            tstateMapper.insert(tstate);
            System.out.println("温度插入成功");

        }

        if(message.matches("HWd[0-9]+Z")){
            String money = message.substring(3,message.length() - 1);
            Profit profit = new Profit();
            LocalDate now = LocalDateTime.now().toLocalDate(); //获取当前时间
            profit.setMoney(money);
            profit.setCreatetTime(now);
            profitMapper.insert(profit);
            System.out.println("营业额插入成功！");
        }

//        if(message.matches("HWid[0-9A-Z]+dcrl\\d+(\\.\\d+)?Z")){
//
//            String arrnews1[] = message.split("id");
//            String arrnew2[] = arrnews1[1].split("dcrl");
//            String arrnew3[] = arrnew2[1].split("Z");
//
//            String V1 = arrnew2[0];  //截取温度
//            String I = arrnew3[0];  //电容
//            Battery battery = new Battery();
//            LocalTime now = LocalDateTime.now().toLocalTime(); //获取当前时间
//            battery.setDcrl(I);
//            battery.setYhid(V1);
//            battery.setCreateTime(now);
//            batteryMapper.insert(battery);
//            System.out.println("电池容量插入成功！");
//        }


//        if(message.substring(0,5).equals("HWPID")){
//            String userid = message.substring(5,message.length() - 1);
//            User user = new User();
//            user.setYhid(userid);
//            user.setYupwd("123456");
//            userMapper.insert(user);
//
//        }
//        if(message.substring(0,5).equals("HWRID")){
//            String userid = message.substring(5,message.length() - 1);
//            User user = new User();
//            userMapper.deleteid(userid);
//        }
//




    }
}




